package com.lenovo.smarttraffic.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

/**
 * Created by Caleb.F
 * on 2019-10-27
 * at 21:35
 */
public class MyImageView extends android.support.v7.widget.AppCompatImageView {
    private Bitmap mbitmap;
    private float totalTranlateX=840f;
    private float totaltranslateY=500f;
    private float totalScareRadio=1.0f;
    private Matrix matrix=new Matrix();
    private float prevDist;
    private GestureDetector gestureDetector;
    private static final String TAG="MyImageView";

    public MyImageView(Context context) {
        super(context);
        initdecgesture();
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initdecgesture();

    }

    public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initdecgesture();

    }

    private void initdecgesture() {
     gestureDetector =new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener()
        {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                float translateX=e2.getX()-e1.getX();
                float translateY=e2.getY()-e1.getY();
                totalTranlateX+=translateX;
                totaltranslateY+=translateY;
                Log.d(TAG, "onFling: "+totalTranlateX);
                Log.d(TAG, "onFling: "+totaltranslateY);
                postInvalidate();
                return false;
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                Log.d(TAG, "onDoubleTap: ======");
                return false;
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                Log.d(TAG, "onDoubleTapEvent: ==============");
                return super.onDoubleTapEvent(e);
            }

            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                Log.d(TAG, "onSingleTapConfirmed: ============");
                return super.onSingleTapConfirmed(e);
            }
        });
        
     
     
    }

    public void setImage(int resourceId){
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
        setImage(bitmap);
    }

    public void setImage(Bitmap bitmap){
        this.mbitmap=bitmap;
        postInvalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getPointerCount()>=2){
            switch (event.getActionMasked()){
                case MotionEvent.ACTION_POINTER_DOWN:
                    prevDist=calculdist(event);
                    break;
                case MotionEvent.ACTION_MOVE:
                    float curDist=calculdist(event);
                    float scaleRadio= curDist / prevDist;
                    totalScareRadio *=scaleRadio;
                    Log.d(TAG, "onTouchEvent: "+totalScareRadio);
                    postInvalidate();
                    prevDist=curDist;
                    break;
            }
        }
        else{
             gestureDetector.onTouchEvent(event);
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        matrix.reset();
        matrix.postScale(totalScareRadio,totalScareRadio);
        matrix.postTranslate(totalTranlateX,totaltranslateY);
        canvas.drawBitmap(mbitmap,matrix, null);
    }

    private float calculdist(MotionEvent event) {
        float x=event.getX(0)-event.getX(1);
        float y=event.getY(0)-event.getY(1);
        return (float) Math.sqrt(x*x+y*y);
    }

}
